﻿#region This file is part of the iTextSharp.Extended.Test library
//
// Program.cs
//
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan) 
// Copyright (c) 2013 Josip Habjan. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>. 
//
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.converter;

namespace iTextSharp.extended.test
{
    public class Program
    {
        static void Main(string[] args)
        {
            string inputFile = @"F:\pdf-test\pdf-in.pdf";
            string outputFile = @"F:\pdf-test\pdf-out.pdf";

            PdfContentToGrayscaleConverter grayscaleConverter = new PdfContentToGrayscaleConverter();

            PdfReader reader = new PdfReader(inputFile);
            
            using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                Document document = new Document(reader.GetPageSizeWithRotation(1));
                PdfWriter writer = PdfWriter.GetInstance(document, fsOutput);

                document.Open();
                PdfContentByte cb = writer.DirectContent;

                int numberOfPages = reader.NumberOfPages;

                for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber++)
                {
                    Rectangle pageSizeWithRotation = reader.GetPageSizeWithRotation(pageNumber);

                    document.SetPageSize(pageSizeWithRotation);
                    document.NewPage();

                    // >>> CONVERT CURRENT PAGE TO GRAYSCALE
                    grayscaleConverter.Convert(reader, pageNumber);
                    // <<<<

                    PdfImportedPage pip = writer.GetImportedPage(reader, pageNumber);

                    if (pageSizeWithRotation.Rotation == 90 || pageSizeWithRotation.Rotation == 270)
                    {
                        cb.AddTemplate(pip, 0, -1f, 1f, 0, 0, pageSizeWithRotation.Height);
                    }
                    else
                    {
                        cb.AddTemplate(pip, 1f, 0, 0, 1f, 0, 0);
                    }
                }

                document.Close(); document.Dispose(); document = null;
                writer.Close(); writer.Dispose(); writer = null;
                reader.Close(); reader.Dispose(); reader = null;
            }
        }
    }
}
